import { Sequelize } from 'sequelize-typescript';
import UserModel from '../models/user.model';
import RoleModel from '../models/role.model';
import PermissionModel from '../models/permission.model';
import UserRoleModel from '../models/user-role.model';
import RolePermissionModel from '../models/role-permission.model';

// 创建 Sequelize 实例
const sequelize = new Sequelize({
  database: process.env.DB_NAME, // 数据库名称
  dialect: 'mysql', // 数据库类型
  username: process.env.DB_USER, // 数据库用户名
  password: process.env.DB_PASSWORD, // 数据库密码
  host: process.env.DB_HOST, // 数据库主机
  logging: console.log, // 是否打印 SQL 日志
  models: [__dirname + '/../models/*.model.ts'], // 指定模型文件路径
});

export const initDatabase = async () => {
  await sequelize.sync({ alter: true }).then(async () => {
    const roles = await RoleModel.bulkCreate([
      {
        name: '管理员',
        remark: '系统管理员，拥有所有权限',
      },
    ]);

    const users = await UserModel.bulkCreate([
      {
        username: 'admin',
        password: '$2b$10$7srqpv9.IDp1CgTy05GuIukjiFiOX6MYi/r3h9FKrZCGItA/DENGy',
        email: '492384481@qq.com',
      },
    ]);

    await UserRoleModel.bulkCreate([{ userId: users[0].id, roleId: roles[0].id }]);

    const permissions = await PermissionModel.bulkCreate([
      { code: 'create_user', name: '允许创建新用户', type: 'api' },
      { code: 'delete_user', name: '允许删除用户', type: 'api' },
      { code: 'view_user', name: '允许查看用户', type: 'api' },
      { code: 'update_user', name: '允许更新用户', type: 'api' },
      { code: 'create_role', name: '允许创建新角色', type: 'api' },
      { code: 'delete_role', name: '允许删除角色', type: 'api' },
      { code: 'view_role', name: '允许查看角色', type: 'api' },
      { code: 'update_role', name: '允许更新角色', type: 'api' },
      { code: 'create_product', name: '允许创建新商品', type: 'api' },
      { code: 'delete_product', name: '允许删除商品', type: 'api' },
      { code: 'view_product', name: '允许查看商品', type: 'api' },
      { code: 'update_product', name: '允许更新商品', type: 'api' },
      { code: 'create_category', name: '允许创建新分类', type: 'api' },
      { code: 'delete_category', name: '允许删除分类', type: 'api' },
      { code: 'view_category', name: '允许查看分类', type: 'api' },
      { code: 'update_category', name: '允许更新分类', type: 'api' },
      { code: 'create_order', name: '允许创建新订单', type: 'api' },
      { code: 'delete_order', name: '允许删除订单', type: 'api' },
      { code: 'view_order', name: '允许查看订单', type: 'api' },
      { code: 'update_order', name: '允许更新订单', type: 'api' },
      { code: 'create_address', name: '允许创建新地址', type: 'api' },
      { code: 'delete_address', name: '允许删除地址', type: 'api' },
      { code: 'view_address', name: '允许查看地址', type: 'api' },
      { code: 'update_address', name: '允许更新地址', type: 'api' },
      { code: 'create_permission', name: '允许创建新权限', type: 'api' },
      { code: 'delete_permission', name: '允许删除权限', type: 'api' },
      { code: 'view_permission', name: '允许查看权限', type: 'api' },
      { code: 'update_permission', name: '允许更新权限', type: 'api' },
    ]);

    permissions.forEach((permission) => {
      RolePermissionModel.create({ roleId: roles[0].id, permissionId: permission.id });
    });
  });
};

export default sequelize;
